package kycadminrepo

import (
	"context"

	"code.bydev.io/cht/customer/kyc_admin_service/internal/pkg/kycadminrepo/o_kyc_record_common"
	"git.bybit.com/svc/dbstructure_user/pkg/o_member_kyc_record"
	"git.bybit.com/svc/go/pkg/bdal/bqb"
	"go.uber.org/zap"
)

func (r *repo) GetKYCRecordCommon(ctx context.Context, memberID int64) (*o_kyc_record_common.Record, error) {
	pool, err := r.userDB.DBDefaultReplicaNodePool(ctx)
	if err != nil {
		r.logger.Error("GetDBDefaultReplicaNodePool Fail", zap.Error(err))
		return nil, err
	}

	selectSQL := o_kyc_record_common.Select().WhereC(func(wb *bqb.WhereBuilder) {
		o_kyc_record_common.FldMemberId.EQ(wb)
	}).LimitC(1).SQL()

	r.logger.Debug("GetKYCRecordCommon sql", zap.Any("selectSQL", selectSQL))
	commonRecord, err := o_kyc_record_common.ScanRecordNil(selectSQL.QueryArgs(ctx, pool, bqb.Args{
		o_kyc_record_common.FldMemberId.Param(): memberID,
	}))

	if err != nil {
		return nil, err
	}

	return commonRecord, nil
}

func (r *repo) GetMemberKYCRecord(ctx context.Context, memberID int64) (*o_member_kyc_record.Record, error) {
	pool, err := r.userDB.DBDefaultReplicaNodePool(ctx)
	if err != nil {
		r.logger.Error("GetDBDefaultReplicaNodePool Fail", zap.Error(err))
		return nil, err
	}

	selectSQL := o_member_kyc_record.Select().WhereC(func(wb *bqb.WhereBuilder) {
		o_member_kyc_record.FldMemberId.EQ(wb)
	}).LimitC(1).SQL()

	commonRecord, err := o_member_kyc_record.ScanRecordNil(selectSQL.QueryArgs(ctx, pool, bqb.Args{
		o_member_kyc_record.FldMemberId.Param(): memberID,
	}))

	if err != nil {
		return nil, err
	}

	return commonRecord, nil
}
